import Vue from 'vue';
import Vuex from 'vuex';

import app from './modules/app';
import user from './modules/user';
import order from './modules/order';
import dictionaries from './modules/dictionaries'
import select from "./modules/select"

Vue.use(Vuex);
const now = new Date();
const store = new Vuex.Store({
    state: {
        wsobj :null,
        workType: '',
        show: false,
        tableHeight: window.innerHeight - 200,
        // 当前用户
        userName: {
            name: 'coffce',
            img: 'dist/images/1.jpg',
        },
        // 会话列表
        sessions: [
            // {
            //     accNo:'',
            //     name: '',
            //     img: '',
            //     messages: [
            //         {
            //             content: '',
            //             date: now,
            //             self:'',
            //             sendSucess:''
            //         }, {
            //             content: '',
            //             date: now,
            //             self:'',
            //             sendSucess:''
            //         }
            //     ]
            // },
        ],
        // messages: [
        //     {
        //         content: '',
        //         date: now
        //     }, {
        //         content: '',
        //         date: now
        //     }
        // ],
        // 当前选中的会话
        currentSessionId: "",
        // 过滤出只包含这个key的会话
        filterKey: '',
        successStatus:'',
        sendContentNew:''
    },
    mutations: {
        changeStatus(state,n) {
            state.show = n;
        },
        successStatus(state,successStatus){
            state.successStatus = successStatus
        },
        sendContentNew(state,sendContentNew){
            state.sendContentNew = sendContentNew
        },
        insertMessage(state,message){
           state.sessions[0].messages = message;
        },
        updatewsobj(state,wsobj){
            state.wsobj = wsobj;
         },
        //
        /* 更新worktypt参数 */
        getworkTypeDate(state, data) {
            state.workType = data;// 之后才是修改state中的状态
        },
        getTableHeightDate(state, data) {
            state.tableHeight = data - 200;// 之后才是修改state中的状态
        },
        // INIT_DATA(state) {
        //     let data = localStorage.getItem('vue-chat-session');
        //     if (data) {
        //         state.sessions = JSON.parse(data);
        //     }
        // },
        // 发送消息
        SEND_MESSAGE({ sessions, currentSessionId }, content) {
            // state.sessions = state.filterKey
            console.log(sessions,"要老命了～")
            console.log(currentSessionId);
            console.log(content);
            let session = sessions.find(item => item.accNo == currentSessionId);
            console.log(session,'ceshi')
            session.messages.push({
                content: content,
                date: new Date(),
                self: true
            });
            currentSessionId = currentSessionId
            localStorage.setItem("messages",JSON.stringify(session.messages));
        },
        // 选择会话
        SELECT_SESSION(state, id) {
            state.currentSessionId = id;
            console.log(state.currentSessionId,'state.currentSessionId')

        },
        // 搜索
        SET_FILTER_KEY(state, value) {
            state.filterKey = value;
            // console.log()
        },
        USER_DATA(state, data){
            state.filterKey = data;
            console.log(state.filterKey)
            console.log(state.sessions,'sessions')
            state.sessions = state.filterKey
        }
    },
    actions: {
        changeTableHeight(ctx, data) {
            ctx.commit('getTableHeightDate', data)
        },
        initData: (context) => context.commit('INIT_DATA'),
        sendMessage: (context, content) => context.commit('SEND_MESSAGE', content),
        selectSession: (context, id) => context.commit('SELECT_SESSION', id),
        // search: ( context, value) => context.commit('SET_FILTER_KEY', value),
        search: ( context, data) => context.commit('USER_DATA', data),
    },
    modules: {
        app,
        user,
        order,
        dictionaries,
		select
    },
});
// store.watch(
//     (state) => state.sessions,
//     (val) => {
//         console.log('CHANGE: ', val);
//         localStorage.setItem('vue-chat-session', JSON.stringify(val));
//     },
//     {
//         deep: true
//     }
// );

export default store;
// export const actions = {
    
// };
